<?php
/**
 * SFKNews
 * 私房库 www.sifangku.com 教学项目
 *
 * @desc 首页
 * @copyright 私房库 www.sifangku.com 保留所有版权
 * @author 孙胜利 www.sunshengli.com
 * @version 1.01
 *
 **/
namespace Home\Controller;
use Common\Controller;
use Home\Model\TypeModel;
use Home\Model\ArticleModel;
use Common\Url;
use Common\Tree;
use Common\Page;
if(!defined('SITE')) exit('Access Denied');
class IndexController extends Controller {
	public function __construct(){
		parent::__construct();
	}
	public function index(){
		$typeModel=new TypeModel();
		$articleModel=new ArticleModel();
		$this->view->setData('nav',$typeModel->selectNav());
		$this->view->setData('h',$articleModel->selectH());
		$this->view->setData('a',$articleModel->selectA());
		$this->view->setData('c',$articleModel->selectC());
		$this->view->display();
	}
	public function type(){
		$typeModel=new TypeModel();
		if(count($nav=$typeModel->selectNavByUname(get('type')))){
			$tree=new Tree($typeModel->getFamily(array('id'=>$nav[0]['id'],'fields'=>'id,pid,tid')));
			$ids=$tree->getPosterity($nav[0]['id'],true);
			$articleModel=new ArticleModel();
			$atids=array();
			foreach ($ids as $val){
				$atids[]=$val['id'];
			}
			$totalData=$articleModel->count(array('atid in('.implode($atids,',').') AND isdelete=0'));
			$url=Url::getUrl(array(M=>'type','uname'=>$nav[0]['uname'],'page_index'=>'{getIndex}','page_num'=>'{pageNum}'));
			$page=new Page($totalData,array('url'=>$url));
			$this->view->setData('articles',$articleModel->selectByAtid($atids,$page->getLimit()));
			$this->view->setData('page',$page->getHtml());
		}else{
			throw new \Exception('栏目不存在.');
		}
		$this->view->display();
	}
	public function article(){
		$articleModel=new ArticleModel();
		if(count($article=$articleModel->selectDetails(get('id'),get('date'),get('type')))){
			$typeModel=new TypeModel();
			$tree=new Tree($typeModel->getFamily(array('id'=>$article[0]['atid'],'fields'=>'id,pid,tid,name,uname')));
			$this->view->setData('location',$tree->getAncestor($article[0]['atid'],true,true));
			$this->view->setData('article',$article[0]);
		}else{
			throw new \Exception('新闻不存在.');
		}
		$this->view->display();
	}
	public function test(){
		
	}
	
}